<template>
  <upload-excel :on-success="handleSuccess" />
</template>

<script>
import { getImportEmployee } from '@/api/employee'
import { formatExcelDate } from '@/utils/index'
export default {
  name: 'Import',
  methods: {
    async handleSuccess({ header, results }) {
    //   console.log(header, results)
    // 格式转换
      const data = this.transformType(results)
      console.log(data)
      // 发送请求
      const { data: res } = await getImportEmployee(data)
      console.log('批量导入员工', res)
      // 跳回页面
      this.$router.back()
    },
    // 格式转换
    transformType(results) {
      const mapInfo = {
        '入职日期': 'timeOfEntry',
        '手机号': 'mobile',
        '姓名': 'username',
        '转正日期': 'correctionTime',
        '工号': 'workNumber',
        '部门': 'departmentName',
        '聘用形式': 'formOfEmployment'
      }
      return results.map(item => {
        const zhkeys = Object.keys(item)
        const obj = {}
        zhkeys.forEach(zhkey => {
          const enkey = mapInfo[zhkey]
          if (enkey === 'timeOfEntry' || enkey === 'correctionTime') {
            // 标准时间
            obj[enkey] = new Date(formatExcelDate(item[zhkey]))
            console.log(obj[enkey])
          } else {
            obj[enkey] = item[zhkey]
          }
        })
        return obj
      })
    }
  }
}
</script>
